﻿using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication2
{
    public partial class TestAccordionGridview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getCategories();
            }
        }

        public void getCategories()
        {
            SqlConnection sqlConn = new SqlConnection(conString);
            sqlConn.Open();
            SqlCommand sqlSelect = new SqlCommand("SELECT * FROM sites", sqlConn);
            sqlSelect.CommandType = System.Data.CommandType.Text;
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlSelect);
            DataSet myDataset = new DataSet();
            sqlAdapter.Fill(myDataset);
            sqlConn.Close();
            Accordion1.DataSource = myDataset.Tables[0].DefaultView;
            Accordion1.DataBind();

        }

        protected void Accordion1_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
        {
            if (e.ItemType == AjaxControlToolkit.AccordionItemType.Content)
            {
                SqlConnection sqlConn = new SqlConnection(conString);
                sqlConn.Open();
                SqlCommand sqlSelect = new SqlCommand("SELECT title FROM articles where siteID = '" + ((HiddenField)e.AccordionItem.FindControl("txt_categoryID")).Value + "'", sqlConn);
                sqlSelect.CommandType = System.Data.CommandType.Text;
                SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlSelect);
                DataSet myDataset = new DataSet();
                sqlAdapter.Fill(myDataset);
                sqlConn.Close();
                GridView grd = new GridView();
                grd = (GridView)e.AccordionItem.FindControl("GridView1");
                grd.DataSource = myDataset;
                grd.DataBind();
            }
        }


        private string conString
        {
            get
            {
                return @"Data Source=autmsvrtest-ro\sql2005;Initial Catalog=kaaTestDB;Persist Security Info=True;User ID=sa;Password=sql2005";
            }
        }
    }
}